package dao;

import javabean.Worker;
import ulity.DBConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Db {
    public List<Worker> getAllDate(){
        ArrayList<Worker> workers = null;
        try {
            workers = new ArrayList<>();
            Connection conn = DBConn.getConn();
            String sql="select * from worker";
            PreparedStatement statement = conn.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                Worker worker = new Worker();
                int id = resultSet.getInt(1);
                String name=resultSet.getString(2);
                String work=resultSet.getString(3);

                worker.setId(id);
                worker.setName(name);
                worker.setWork(work);
                workers.add(worker);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return workers;
    }
    public boolean isAdmin(String user,String password) throws Exception{
        Connection conn = DBConn.getConn();
        String sql="select * from admin where user=? and password=?";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1,user);
        statement.setString(2,password);
        ResultSet resultSet = statement.executeQuery();
        return resultSet.next();
    }
    public int addData(Worker worker){
        Connection conn = DBConn.getConn();
        int i= 0;
        try {
            String sql="insert into worker values(null,?,?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,worker.getName());
            statement.setString(2,worker.getWork());
            i = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    public int delectData(String id){
        Connection conn = DBConn.getConn();
        String sql="delete from worker where id=?";
        int i= 0;
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,id);
            i = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    public Worker getOneData(String id){
        Connection conn = DBConn.getConn();
        String sql="select * from worker where id=?";
        Worker worker = new Worker();
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,id);
            ResultSet resultSet = statement.executeQuery();
            resultSet.next();
            worker.setId(resultSet.getInt(1));
            worker.setName(resultSet.getString(2));
            worker.setWork(resultSet.getString(3));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return worker;
    }

    public int upDateDataOk(Worker worker){
        Connection conn = DBConn.getConn();
        String sql="update worker set name=?,work=? where id=?";
        int i = 0;
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,worker.getName());
            statement.setString(2,worker.getWork());
            statement.setInt(3,worker.getId());
            i = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
